package com.jlastu.androidsx.ui;

import android.annotation.SuppressLint;
import android.os.StrictMode;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.jlastu.androidsx.R;
import com.jlastu.androidsx.bean.Communication;
import com.jlastu.androidsx.databinding.ActivityWebviewBinding;

import cn.project.base.baseui.BaseNormalActivity;

public class WebViewActivity extends BaseNormalActivity<ActivityWebviewBinding> {

    Communication communication;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_webview;
    }

    @Override
    protected void initView() {
        communication = (Communication) getIntent().getSerializableExtra("communication");
        mBinding.toolbar.rlBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mBinding.toolbar.tvTitle.setText(communication.getTitle());
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
    }

    @Override
    protected void initData() {
        setWebview();
        mBinding.webview.loadUrl(communication.getWeb_url());
    }

    //配置webView
    @SuppressLint("SetJavaScriptEnabled")
    private void setWebview() {

        mBinding.webview.setWebViewClient(new WebViewClient());
        WebSettings webSettings = mBinding.webview.getSettings();
        //启用js
        webSettings.setJavaScriptEnabled(true);
        //当界面含有http和httpS两种类型图片时，解决部分图片不显示
        webSettings.setBlockNetworkImage(false);
        //设置可以访问文件
        webSettings.setAllowFileAccess(true);

        //设置缓存模式
        webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
        // 开启DOM storage API 功能
        webSettings.setDomStorageEnabled(true);
        // 开启database storage API功能
        webSettings.setDatabaseEnabled(true);
        //解决部分图片不加载
        //配置安全源站尝试从不安全源站加载资源时WebView的行为
        webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        webSettings.setSupportMultipleWindows(false);

        WebView.setWebContentsDebuggingEnabled(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setUseWideViewPort(true);//宽屏
        webSettings.setSupportZoom(true);//支持缩放

    }
}
